package com.bole.controller;


import com.bole.dto.car.BrowsingHistoryDto;
import com.bole.dto.car.TbCarDto;
import com.bole.pojo.BrowsingHistoryDo;
import com.bole.service.BrowsingHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@Api(value = "浏览记录api")
@RequestMapping("broHis")
@RestController
public class BrowsingHistoryController {

    @Autowired
    private BrowsingHistoryService browsingHistoryService;



    //通过userId查询所有
    @PostMapping("selectByUserId")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataTypeClass = Long.class)
    public List<BrowsingHistoryDto> selectByUserId(@RequestParam("userId") Long userId) {
        List<BrowsingHistoryDo> browsingHistoryDos = browsingHistoryService.selectByUserId(userId);
        List<BrowsingHistoryDto> browsingHistoryDtos = new ArrayList<>();
        for (BrowsingHistoryDo browsingHistoryDo : browsingHistoryDos) {
            BrowsingHistoryDto browsingHistoryDto = new BrowsingHistoryDto();
            BeanUtils.copyProperties(browsingHistoryDo,browsingHistoryDto);
            browsingHistoryDtos.add(browsingHistoryDto);
        }
        return browsingHistoryDtos;
    }

    //保存
    @PostMapping("saveHistory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataTypeClass = Long.class)
    })
    public void saveHistory(@RequestParam("userId") Long userId,@RequestParam("carId") Long carId) {
        browsingHistoryService.insertHistory(userId,carId);
    }
}
